﻿#region Using
using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; 
#endregion

namespace AifMessageHeaderEditor.Lib
{
    internal class AppCommon
    {
        internal const string XML_NS = "ns";
        internal const string AIF_XPATH_NS = "/ns";
        internal const string AIF_XPATH_HEADER = "/ns:Envelope/ns:Header";
        internal const string AIF_XPATH_BODY = "/ns:Envelope/ns:Body";
        internal const string AIF_NAMESPACE_DOCUMENTS = 
            "http://schemas.microsoft.com/dynamics/2008/01/documents/Message";

        internal const string AIF_BODY = "Body";
        internal const string AIF_HEADER = "Header";

        internal const string AIF_HEADER_MESSAGEID = "MessageId";
        internal const string AIF_HEADER_SOURCEENDPOINT = "SourceEndpoint";
        internal const string AIF_HEADER_DESTINATIONENDPOINT = "DestinationEndpoint";
        internal const string AIF_HEADER_ACTION = "Action";
        internal const string AIF_HEADER_SOURCEPOINTENDUSER = "SourceEndpointUser";
        internal const string AIF_HEADER_CONVERSATIONID = "ConversationId";
        internal const string AIF_HEADER_REQUESTMESSAGEID = "RequestMessageId";

        internal const string AIF_FILE_SOURCE = "SourceFilename";
        internal const string AIF_FILE_DESTINATION = "DestinationDirectory";
        internal const string AIF_FILE_SOURCE_FILEOWNER = "FileOwner";
        internal const string AIF_FILE_SOURCE_FILEOWNERNAME = "FileOwnerName";
        internal const string AIF_USER_UNKNOWN = "The specified user couldn't be located: ";

        internal const string FILE_EXCEPTION_SOURCE =
            "The following exception occured for the source-file: ";
        internal const string FILE_EXCEPTION_DESTINATION =
            "The following exception occured for the destination-file: ";
        internal const string FILE_EXCEPTION_NOFILENAME =
            "A filename must be specified.";
        internal const string FILE_EXCEPTION_NOUSERNAME =
            "A filename must be specified.";

        internal static bool IsFileLocked(IOException exception)
        {
            int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
            return errorCode == 32 || errorCode == 33;
        }

        static public XmlNode SelectXmlHeaderNode(XmlDocument xmlMessage, string element)
        {
            XmlNode root = xmlMessage.DocumentElement;
            XmlNamespaceManager namespaces = new XmlNamespaceManager(xmlMessage.NameTable);
            namespaces.AddNamespace(AppCommon.XML_NS,
                AppCommon.AIF_NAMESPACE_DOCUMENTS);

            return root.SelectSingleNode(AppCommon.AIF_XPATH_HEADER +
                (element == string.Empty ? string.Empty : AppCommon.AIF_XPATH_NS + ":" + element),
                namespaces);
        }

        public static XmlNode SelectXmlHeaderNode(XmlDocument xmlMessage,
    XmlNamespaceManager namespaces,
    string element = "")
        {
            XmlNode root = xmlMessage.DocumentElement;
            return root.SelectSingleNode(AppCommon.AIF_XPATH_HEADER +
                (element == string.Empty ? string.Empty : AppCommon.AIF_XPATH_NS + ":" + element),
                namespaces);
        }

        public static XmlNode SelectXmlBodyNode(XmlDocument xmlMessage,
            XmlNamespaceManager namespaces)
        {
            XmlNode root = xmlMessage.DocumentElement;
            return root.SelectSingleNode(AppCommon.AIF_XPATH_BODY, namespaces);
        }
    }
}
